﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Produce;
using AMM.Model;
using AMM.Model.Produce;

namespace AMM.Business.Produce {
    public class ProducePlanProcessor : BaseProcessor {

        private int _pageSize;
        private ProducePlanRepository _ProducePlanRepository;

        public ProducePlanProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._ProducePlanRepository = new ProducePlanRepository(pageSize);
        }

        public ProducePlan FetchProducePlanById(int producePlanId) {
            try {
                return this._ProducePlanRepository.FetchProducePlanById(producePlanId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定生产计划失败！");
            }
        }

        public List<ProducePlan> FetchProducePlanByProduceConductId(int produceConductId) {
            try {
                return this._ProducePlanRepository.FetchProducePlanByProduceConductId(produceConductId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定生产计划失败！");
            }
        }

        public List<ProducePlan> FetchProducePlanForViewByProduceConductId(int produceConductId) {
            try {
                return this._ProducePlanRepository.FetchProducePlanForViewByProduceConductId(produceConductId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定生产计划失败！");
            }
        }

        public List<ProducePlan> FetchProducePlanForView() {
            try {
                return this._ProducePlanRepository.FetchProducePlanForView();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定生产计划失败！");
            }
        }

        public List<ProducePlanViewModel> FetchProducePlanViewModelByProduceConductIdList(int produceConductId) {
            try {
                List<ProducePlan> resultList = this._ProducePlanRepository.FetchProducePlanForViewByProduceConductId(produceConductId);
                return resultList.Select(x => {
                    return new ProducePlanViewModel() {
                        Key = x.ProducePlanId,
                        PlanDate = x.PlanDate,
                        PlanAmount = x.PlanAmount,
                        ProduceUnitCode = x.Basic.Code,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取生产计划列表失败！");
            }
        }

        public List<ProduceConductForPlanViewModel> FetchProduceConductForPlanViewModelList(ProducePlanSearchModel searchModel, int pageIndex = 1) {
            try {
                List<ProduceConduct> resultList = this._ProducePlanRepository.FetchProduceConductForPlanList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new ProduceConductForPlanViewModel() {
                        Key = x.ProduceConductId,
                        BatchAmount = x.BatchAmount,
                        CustomerOrderCode = x.CustomerOrder.OrderNo,
                        ProduceConductCode = x.Code,
                        ProductCode = x.Product.Code,
                        PlanAmount = x.ProducePlans.Where(y => y.ProduceConductId == x.ProduceConductId).Sum(y => y.PlanAmount)
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取生产计划列表失败！");
            }
        }

        public void Update(List<ProducePlan> producePlanList) {
            try {
                producePlanList.ForEach(x => {
                    if (x.ClassStatus == ClassStatus.Added) {
                        x.ProducePlanId = Guid.NewGuid().GetHashCode();
                    }
                    else {
                        ProducePlan oldProducePlan = this._ProducePlanRepository.FetchProducePlanById(x.ProducePlanId);
                        if (!oldProducePlan.RowVersion.SequenceEqual(x.RowVersion)) {
                            throw this.BuildException("更新生产计划失败，数据不一致性错误！");
                        }
                    }
                });
                this._ProducePlanRepository.UpdateProducePlan(producePlanList);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("保存生产计划失败！");
            }
        }

        public void Delete(ProducePlan ProducePlan) {
            try {
                this._ProducePlanRepository.Delete(ProducePlan);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除生产计划失败！");
            }
        }

        public void Delete(params int[] ProducePlanIds) {
            try {
                this._ProducePlanRepository.DeleteMultipleProducePlan(ProducePlanIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除生产计划失败！");
            }
        }

    }
}
